import { Component,Injectable} from '@angular/core';
import { PersonalService} from './personal.service';
import { PersonalInfo} from '../../services/model/personalInfo';
import {Grade} from "../../services/model/grade";
import {Major} from "../../services/model/major";
import {Nation} from "../../services/model/nation";
import {PopService} from "dolphinng";
@Component({
    selector: 'personal',
    templateUrl: './personal.component.html',
    styleUrls: ['./personal.component.less'],
    providers:[PersonalService,PopService]
})
@Injectable()
export class PersonalComponent {
  tableData:any[];
  page:{
    pageIndex:number,
    pageSize:number,
    count:number
  };
  searchParams:{
    studentId?:number,
    studentName?:string,
    gender?:string,
    majorId?:number,
    gradeId?:number,
    address?:string,
    mobile?:string,
    homePhone?:string
  };
  majors:Major[];
  grades:Grade[];
  nations:Nation[];
  majorOptions:Major[];
  gradeOptions:Grade[];

  modalAdd={
    visible:false,
    submitted:false,
    data:new PersonalInfo(),
    nationOptions:[],
    majorOptions:[],
    gradeOptions:[]
  };
  modalEdit={
    visible:false,
    submitted:false,
    data:new PersonalInfo(),
    nationOptions:[],
    majorOptions:[],
    gradeOptions:[]
  };
  constructor(private personalSvc:PersonalService,private pop:PopService){
    this.resetParams();
    this.getNations();
    this.getGrades();
    this.getMajors();
    this.page={
        pageIndex:0,
        pageSize:10,
      count:0
    };
  }
  getNations(){
    this.personalSvc.getNations()
      .then((data)=>{
        if(data.length) {
          this.nations=data;
          console.log(data);
        }
      })
  }
  getGrades(){
    this.personalSvc.getGrades()
      .then((data)=>{
        if(data.length) {
          this.grades=data;
          let grade=new Grade();
          grade.gradeId=-1;
          grade.name='不限';
          this.gradeOptions=[grade].concat(data);
          console.log(this.gradeOptions);
        }
      })
  }
  getMajors(){
    this.personalSvc.getMajors()
      .then((data)=>{
        if(data.length) {
          this.majors = data;
          let major=new Major();
          major.majorId=-1;
          major.major='不限';
          this.majorOptions=[major].concat(data);
        }
        console.log(this.majorOptions);
      })
  }
  search(){
    this.page.pageIndex=0;
    this.page.count=1;
    this.tableData=[];
    this.query();
  }
  query(){
    let params={
      studentId:this.searchParams.studentId,
      studentName:this.searchParams.studentName,
      gender:this.searchParams.gender,
      majorId:this.searchParams.majorId,
      gradeId:this.searchParams.gradeId,
      address:this.searchParams.address,
      mobile:this.searchParams.mobile,
      homePhone:this.searchParams.homePhone,
      pageIndex:this.page.pageIndex,
      pageSize:this.page.pageSize
    };
    this.personalSvc.query(params)
      .then((res)=>{
        console.log(res);
        this.tableData=res.items;
        this.page.count=res.count;
      });
  }
  resetParams(){
    this.searchParams={
      gender:'',
      majorId:-1,
      gradeId:-1,
    }
  }
  showAddModal(){
    this.modalAdd.data=new PersonalInfo();
    this.modalAdd.data.gender='男';
    this.modalAdd.data.gradeId=-1;
    this.modalAdd.data.majorId=-1;
    this.modalAdd.submitted=false;
    let grade=new Grade();
    grade.gradeId=-1;
    grade.name='请选择';
    let major=new Major();
    major.majorId=-1;
    major.major='请选择';

    this.modalAdd.nationOptions=this.nations;
    this.modalAdd.gradeOptions=[grade].concat(this.grades);
    this.modalAdd.majorOptions=[major].concat(this.majors);
    this.modalAdd.data.nationId=this.modalAdd.nationOptions[0].nationId;
    this.modalAdd.visible=true;
  }
  closeAddModal(){
    this.modalAdd.visible=false;
  }
  showEditModal(p:PersonalInfo){
    this.modalEdit.data=new PersonalInfo();
    this.modalEdit.data.id=p.id;
    this.modalEdit.data.studentId=p.studentId;
    this.modalEdit.data.name=p.name;
    this.modalEdit.data.gender=p.gender;
    this.modalEdit.data.gradeId=p.gradeId;
    this.modalEdit.data.majorId=p.majorId;
    this.modalEdit.data.birthday=p.birthday;
    this.modalEdit.data.nationId=p.nationId;
    this.modalEdit.data.address=p.address;
    this.modalEdit.data.mobile=p.mobile;
    this.modalEdit.data.homePhone=p.homePhone;
    this.modalEdit.submitted=false;
    let grade=new Grade();
    grade.gradeId=-1;
    grade.name='请选择';
    let major=new Major();
    major.majorId=-1;
    major.major='请选择';
    this.modalEdit.nationOptions=this.nations;
    this.modalEdit.gradeOptions=[grade].concat(this.grades);
    this.modalEdit.majorOptions=[major].concat(this.majors);
    this.modalEdit.visible=true;
  }
  closeEditModal(){
    this.modalEdit.visible=false;
  }
  add(){
    let valid=this.testPersonalInfo(this.modalAdd.data);
    if(valid){
      this.modalAdd.submitted=true;
      this.personalSvc.add(this.modalAdd.data)
        .then((data)=>{
          this.modalAdd.submitted=false;
          this.modalAdd.visible=false;
          if(data.status){
            this.pop.info({
              text:'添加成功！',
              confirmButtonType:'success'
            });
            this.query();
          }else{
            this.pop.error({
              title:'错误',
              text:data.message||'添加失败！'
            });
          }
        })
    }
  }
  update(){
    let valid=this.testPersonalInfo(this.modalEdit.data);
    if(valid){
      this.modalEdit.submitted=true;
      this.personalSvc.update(this.modalEdit.data)
        .then((data)=>{
          this.modalEdit.submitted=false;
          this.modalEdit.visible=false;
          if(data.status){
            this.pop.info({
              text:'修改成功！',
              confirmButtonType:'success'
            });
            this.query();
          }else{
            this.pop.error({
              title:'错误',
              text:data.message||'修改失败！'
            });
          }
        })
    }
  }
  remove(id:number){
    this.pop.confirm({
      text:'确定要删除这条记录？',
      closeOnConfirm:false,
      showLoaderOnConfirm:true
    }).onConfirm(()=>{
      this.personalSvc.remove(id)
        .then((data)=>{
          if(data.status){
            this.pop.info({
              text:'删除成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'删除失败！',
              text:data.message
            });
          }
        });
    })
  }

  testPersonalInfo(info:PersonalInfo):boolean{
    console.log(info.majorId);
    if(!info.studentId){
      this.pop.error({
        title:'提交错误',
        text:'请输入学号！'
      });
      return false;
    }else if(!info.name){
      this.pop.error({
        title:'提交错误',
        text:'请输入姓名！'
      });
      return false;
    }else if(info.gradeId==-1){
      this.pop.error({
        title:'提交错误',
        text:'请选择年级！'
      });
      return false;
    }else if(info.majorId==-1){
      this.pop.error({
        title:'提交错误',
        text:'请选择专业！'
      });
      return false;
    }
    return true;
  }
}
